[#ftl]
[@bootstrapChains "Step2/3 Initializing EditorFactory"]
agile.singoton("agile.EditorFactory",{			
	/*a pool of djiit form widgets reused to edit widget attribute values*/
	pool:{
		/*
		 * editorClass:[dijitFormWidget...]
		 * "dijit.form.TextBox":[dijitFormTextBox1,dijitFormTextBox2,...]
		 */
	}	
	,lookupEditorWidget:function(/*object*/editorMetadata,/*object*/context){
		
		if(this.pool[editorMetadata.editorClass] == null){
			this.pool[editorMetadata.editorClass] = [];
		}
		
		var widgets = this.pool[editorMetadata.editorClass];		
		
		//lookup an idle widget from the pool
		var idleWidget = null;
		for(var i = 0; i < widgets.length; i++){
			if(widgets[i].context == null){
				idleWidget = widgets[i];
				////console.log(lang.replace("found a {0} idle instance from pool",[editorMetadata.editorClass]));
				break;
			}
		}
				
		//create a new widget if none found in the pool
		if(idleWidget == null){			
			idleWidget = this.createEditorWidget(editorMetadata,context);
		}	
		else{			
			//this.initEditorWidget(idleWidget,editorMetadata,context);			
			idleWidget.initialize(editorMetadata,context);
		}
		
		return idleWidget;
	}
	,createEditorWidget:function(editorMetadata,context){
		//var widget = editorMetadata.newInstance(context);
		var widget = this.newInstance(editorMetadata,context);
		//put the new widget to pool for reuse				
		this.pool[editorMetadata.editorClass].push(widget);
		////console.log(lang.replace("create a new {0} instance",[editorMetadata.editorClass]));		
		return widget;
	}
	
	,newInstance:function(metadata,context){
		//var editorWidget = new agile.EditorWidget({editorConfig:metadata,context:context});
		var editorWidget = null;
		if(metadata.isSimple){
			editorWidget = new agile._EditorWidget({editorMetadata:metadata,context:context});
		}else{
			editorWidget = new agile._XEditorWidget({editorMetadata:metadata,context:context});
		}
//		console.log("@@@@@@@editorWidget=",editorWidget.declaredClass,metadata);
		editorWidget.startup();		
		return editorWidget;
	}
});
[/@]
